const fs = require('fs').promises;
const { Client } = require('pg');

const client = new Client({
  host: "127.0.0.1",
  user: "postgres",
  password: "whuyiyulu666666",
      //此处是你添加的数据库名
  database: "animalsmigration",
  	// multipleStatements: true,
  port : 5432,
});

client.connect(); // 建立连接

async function readFile(fileId, outputPath) {
    try {
        const res = await client.query(
            `SELECT file_name, file_data 
             FROM media_files WHERE id = $1`,
            [fileId]
        );
        
        if (res.rows.length === 0) {
            throw new Error('文件不存在');
        }

        const { file_name, file_data } = res.rows[0];
        await fs.writeFile(outputPath, file_data);
        console.log(`文件已保存到: ${outputPath}`);
    } catch (err) {
        console.error('读取失败:', err.message);
    }
}

// 示例：读取 ID=1 的文件
readFile(11, './Knowledge_git/documents/homepage(1).mp4');